<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<pre>
    浅拷贝 Es6 ===>> Object.assign()

    //浅拷贝---自己实现
    function shallowCopy(obj){
        let obj2 = Array.isArray(obj) ? [] : {}
        for (let i in obj){
            obj2[i] = obj[i]
        }
        return obj2
    }

    let obj1 = {
        a:1,
        b:{
            b1:'b-1'
        }
    }
    let obj2 = shallowCopy(obj1)
    let obj3 = obj1
    obj1.a = 2
    console.log(obj1.a,obj2.a,obj3.a) //2  1  2   拷贝成功
    obj1.b.b1 = 'b-11'
    console.log(obj1.b.b1,obj2.b.b1,obj3.b.b1) //b-11 b-11 b-11  不能拷贝深层次
</pre>
<script>

    //浅拷贝
    function shallowCopy(obj){
        let obj2 = Array.isArray(obj) ? [] : {}
        for (let i in obj){
            obj2[i] = obj[i]
        }
        return obj2
    }

    let obj1 = {
        a:1,
        b:{
            b1:'b-1'
        }
    }
    let obj2 = shallowCopy(obj1)
    let obj3 = obj1
    obj1.a = 2
    console.log(obj1.a,obj2.a,obj3.a) //2  1  2   拷贝成功
    obj1.b.b1 = 'b-11'
    console.log(obj1.b.b1,obj2.b.b1,obj3.b.b1) //b-11 b-11 b-11  不能拷贝深层次




</script>
</body>
</html>